import React from "react";
import { Modal, Select, Form, Input, message, Button, DatePicker } from "antd";
const { Option } = Select;

interface PurchaseNotificationValues {
  product_id: number;
  recipient_id: number;
  quantity: number;
}

interface PurchaseNotificationModalProps {
  productList: any[];
  title: string;
  visible: boolean;
  onCancel: () => void;
  onOk: (values: any) => void;
  defaultValue?: any;
  users: any[];
}

const PurchaseNotificationModal: React.FC<PurchaseNotificationModalProps> = ({
  title,
  visible,
  onCancel,
  onOk,
  defaultValue,
  productList,
  users = [],
}) => {
  const [form] = Form.useForm<PurchaseNotificationValues>();

  const handleOk = async () => {
    try {
      const values = await form.validateFields();
      onOk(values);
      form.resetFields();
    } catch (errorInfo) {
      console.log("Failed:", errorInfo);
    }
  };

  return (
    <Modal
      title={title}
      visible={visible}
      onOk={handleOk}
      onCancel={onCancel}
      footer={[
        <Button key="cancel" onClick={onCancel}>
          取消
        </Button>,
        <Button key="ok" type="primary" onClick={handleOk}>
          确定
        </Button>,
      ]}
    >
      <Form form={form} initialValues={{ ...defaultValue }}>
        <Form.Item
          label="选择产品"
          name="product_id"
          rules={[
            {
              required: true,
              message: "请选择产品",
            },
          ]}
        >
          <Select placeholder="请选择产品">
            {productList.map((product) => (
              <Option key={product.id} value={product.id}>
                {product.product_name}
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label=" 接收人"
          name="recipient_id"
          rules={[
            {
              required: true,
              message: "请选择采购人员",
            },
          ]}
        >
          {/* Add a Select component for selecting suppliers */}
          <Select placeholder="请选择采购人员">
            {/* Map through the 'users' array to display supplier options */}
            {users.map((user) => (
              <Option key={user.id} value={user.id}>
                {user.username}
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label="数量"
          name="quantity"
          rules={[
            {
              required: true,
              message: "请输入有效的数量",
            },
          ]}
          validateTrigger="onBlur"
        >
          <Input type="number" placeholder="请输入数量" />
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default PurchaseNotificationModal;
